﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Services
{
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Repositories;
	using Falcon.Core.Domain.Services;
	using Falcon.Core.Domain.Values;
	using Falcon.Core.Extensions;

	public class UserProfileService : IUserProfileService
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="UserProfileService"/> class.
		/// </summary>
		/// <param name="userProfileRepository">The user profile repository.</param>
		public UserProfileService(IUserProfileRepository userProfileRepository)
		{
			this.UserProfileRepository = userProfileRepository;
		}

		/// <summary>
		/// Gets or sets the user profile repository.
		/// </summary>
		/// <value>The user profile repository.</value>
		public IUserProfileRepository UserProfileRepository
		{
			get; 
			private set;
		}

		/// <summary>
		/// Gets the user profile using the specified specification.
		/// </summary>
		/// <param name="specification">The specification.</param>
		/// <returns></returns>
		public UserProfile GetBy(UserProfileSpecification specification)
		{
			return this.UserProfileRepository.GetBy(specification);
		}

		/// <summary>
		/// Gets the user profile by id.
		/// </summary>
		/// <param name="id">The id.</param>
		/// <returns></returns>
		public UserProfile GetById(int id)
		{
			return this.UserProfileRepository.GetById(id);
		}

		/// <summary>
		/// Creates the user profile.
		/// </summary>
		/// <returns></returns>
		public UserProfile Create()
		{
			return new UserProfile();
		}

		/// <summary>
		/// Inserts the user profile into the storage.
		/// </summary>
		/// <param name="profile">The <see cref="UserProfile"/>.</param>
		public void Insert(UserProfile profile)
		{
			profile.Validate();

			if (profile.Signature.HasValue())
			{
				profile.SignatureParsed = profile.Signature.BBCodeToHtml(true);
			}

			this.UserProfileRepository.Insert(profile);
		}

		/// <summary>
		/// Updates the specified user profile in the storage.
		/// </summary>
		/// <param name="profile">The <see cref="UserProfile"/>.</param>
		public void Update(UserProfile profile)
		{
			profile.Validate();

			if (profile.Signature.HasValue())
			{
				profile.SignatureParsed = profile.Signature.BBCodeToHtml(true);
			}

			this.UserProfileRepository.Update(profile);
		}

		/// <summary>
		/// Updates the specified user profile in the storage.
		/// </summary>
		/// <param name="profile">The profile.</param>
		/// <param name="value">The value.</param>
		public void Update(UserProfile profile, UserProfileUpdate value)
		{
			value.Validate();

			value.ToEntity(profile);

			this.Update(profile);
		}

		/// <summary>
		/// Deletes the specified user profile in the storage.
		/// </summary>
		/// <param name="profile">The <see cref="UserProfile"/>.</param>
		public void Delete(UserProfile profile)
		{
			this.UserProfileRepository.Delete(profile);
		}
	}
}